package com.example.myapplication.slice.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.slice.ProviderTabPageSliderProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class MainPageAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_zero_ability_main);

        //初始化TaList
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        String[] tabListTags = {"首页", "测试", "查找", "添加"};
        for (int i = 0; i < tabList.getTabLength(); i++) {
            TabList.Tab tab = tabList.new Tab(this);
            tab.setText(tabListTags[i]);
            tabList.addTab(tab);
        }

        //初始化PageSlider
        List<Integer> layoutFileIds = new ArrayList<>();
        layoutFileIds.add(ResourceTable.Layout_APP_ability_main1);
        layoutFileIds.add(ResourceTable.Layout_picture_ability_main1);
        layoutFileIds.add(ResourceTable.Layout_ability_search);
        layoutFileIds.add(ResourceTable.Layout_ability_main_add);
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setProvider(new ProviderTabPageSliderProvider(layoutFileIds,this));



        //3.TabList与PageSlider联动
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                //获取点击的菜单的索引
                int index = tab.getPosition();
                //设置pageSlider的索引与菜单索引一致
                pageSlider.setCurrentPage(index);
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });

    }
}
